package com.bk.bookviews.util;


/**
 * 字符串工具类
 */
public class StringUtil {
    /**
     * @description 判断字符串是否为空
     * @param str  字符串
     * @return boolean
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * @description 判断字符串是否不为空
     * @param str  字符串
     * @return boolean
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * @description 判断是否为空，去掉前后空格
     * @param str  字符串
     * @return boolean
     */
    public static boolean isBlank(String str) {
        return isEmpty(str==null?"":str.trim());
    }

    /**
     * @description 判断是否不为空，去掉前后空格
     * @param str  字符串
     * @return boolean
     */
    public static boolean isNotBlank(String str) {
        return isNotEmpty(str==null?"":str.trim());
    }

    /**
     * @description 重复一个字符串次数
     * @param str  字符串
     * @param repeat  重复次数
     * @return String
     */
    public static String repeat(String str, int repeat) {
        return repeat(str,"",repeat);
    }

    /**
     * @description 重复一个字符串次数，用指定分割符拼接
     * @param str  字符串
     * @param repeat  重复次数
     * @return String
     */
    public static String repeat(String str, String separator, int repeat) {
        if (isEmpty(str)) {
            throw new RuntimeException("--------->the param must not be null!");
        }
        if (repeat <= 0) {
            return "";
        }
        if(repeat>30){
            throw new RuntimeException("--------->at most only allowed repeat 30 times");
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<repeat;i++){
            sb.append(str).append(separator);
        }
        //如果分割符不为空，则删除最后一个字符
        if(isNotEmpty(separator)){
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }

    /**
     * @description 对源对象进行长度右补充
     * @param obj  源对象
     * @param length  补充长度
     * @param padChar 补充字符
     * @return String
     */
    public static String rightPad(Object obj, int length, char padChar) {
        if (obj == null) {
            return null;
        }
        if(length>=50){
            throw new RuntimeException("--------->at most only allowed pad 50 bits");
        }
        String str = obj.toString();
        int pads = length-str.length();
        if (pads <= 0) {
            return str;
        }
        return str.concat(repeat(Character.toString(padChar),pads));
    }

    /**
     * @description 对源对象进行长度左补充
     * @param obj  源对象
     * @param length  补充长度
     * @param leftChar 补充字符
     * @return String
     */
    public static String leftPad(Object obj, int length, char leftChar) {
        if (obj == null) {
            return null;
        }
        if(length>=50){
            throw new RuntimeException("--------->at most only allowed pad 50 bits");
        }
        String str = obj.toString();
        int pads = length-str.length();
        if (pads <= 0) {
            return str;
        }
        return repeat(Character.toString(leftChar),pads).concat(str);
    }

    /**
     * @description 查找源字符串中有多少匹配的字符
     * @param str  源字符串
     * @param matchStr  匹配字符串
     * @return int
     */
    public static int countMatches(String str, String matchStr) {
        if (isEmpty(str) || isEmpty(matchStr)) {
            return 0;
        }
        int count = 0;
        int idx = 0;

        while ((idx = str.indexOf(matchStr, idx)) != -1) {
            count++;
            idx += matchStr.length();
        }
        return count;
    }

    /**
     * @description 判断字符串是否都为数字(不含符号)
     * @param str  源字符串
     * @return boolean
     */
    public static boolean isDigit(String str) {
        if (str == null) {
            return false;
        }

        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * @description 判断字符串是否为数字（带小数点）
     * @param str  源字符串
     * @return boolean
     */
    public static boolean isDouble(String str) {
        if (str == null) {
            return false;
        }

        try{
            Double.parseDouble(str);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println("repeat==========="+repeat("abc",",",3));
        System.out.println("rightPad========="+rightPad(50,6,'*'));
        System.out.println("leftPad========="+leftPad(50,6,'*'));
        System.out.println("countMatches==========="+countMatches("abcaaabca","a"));
        System.out.println("isDigit============="+isDigit("12.8"));
        System.out.println("isDouble============="+isDouble("12.8a"));
        
    }
}
